package com.lagouedu.rabbitmq_demo.controller;

import com.lagouedu.rabbitmq_demo.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @RequestMapping("/order")
    public Map order(Integer orderId,Integer productId){
        Map<String,Object> params = new HashMap<>();
        params.put("productId", productId);
        params.put("orderId", orderId);
        return orderService.order(params);
    }

    @RequestMapping("/pay/{orderId}")
    public Map pay(@PathVariable(name = "orderId") Integer orderId){
        Map<String, Object> re = new HashMap<>();
        int orderStatus = orderService.getOrderStatus(orderId);
        if(orderStatus == 3){
            re.put("code",0);
            re.put("msg","订单信息不存在");
            return re;
        } else
        if(orderStatus == 2){
            re.put("code",0);
            re.put("msg","订单已取消请重新下单");
            return re;
        } else
        if(orderStatus == 1){
            re.put("code",0);
            re.put("msg","订单已支付,请勿重复支付");
            return re;
        } else
        {
            // 修改订单状态
            orderService.updateOrderStatus(orderId, 1);
            re.put("code",1);
            re.put("msg","订单支付成功");
            return re;
        }
    }

    @RequestMapping("/checkOrderStatus/{orderId}")
    public Map checkOrderStatus(@PathVariable(name = "orderId") Integer orderId){
//        Assert.isNull(orderId,"orderId不能为空");
        Map<String, Object> re = new HashMap<>();
        int orderStatus = orderService.getOrderStatus(orderId);
        if(orderStatus == 3){
            re.put("code",0);
            re.put("msg","订单信息不存在");
            return re;
        } else
        if(orderStatus == 2){
            re.put("code",0);
            re.put("msg","订单已取消请重新下单");
            return re;
        } else
        if(orderStatus == 1){
            re.put("code",1);
            re.put("msg","订单已支付");
            return re;
        } else {
            re.put("code",0);
            re.put("msg", "支付出现问题，请重新支付");
            return re;
        }
    }

}
